package Interface.Game;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;

public class JDialogColorChooser extends JDialog {
    private JButton componentToChange;

    public JDialogColorChooser(ArrayList<Color> listColors,JButton componentToChange) {
        super();
        this.setTitle("Sélection de la couleur à jouer");
        this.setModal(true);
        this.setSize(300,200);
        this.setResizable(false);
        this.getContentPane().setBackground(Color.WHITE);
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        //centrer la frame
        this.setLocation((int) (screenDimension.getWidth() - this.getWidth()) / 2, (int) (screenDimension.getHeight() - this.getHeight()) / 2);
        this.componentToChange=componentToChange;

        this.setLayout(new GridLayout(3,5, 5, 5));
        for (int iColor = 0; iColor < listColors.size(); iColor++) {
            JButton jbtColor = new JButton();
            jbtColor.setBorder(BorderFactory.createLineBorder(Color.black));
            jbtColor.setBackground(listColors.get(iColor));
            jbtColor.addActionListener(new ActionJDColorChooser());
            this.add(jbtColor);

        }

        JButton jbtNoColor=new JButton(new ImageIcon("./images/noColor.jpg"));
        jbtNoColor.setActionCommand("noColor");
        jbtNoColor.setBackground(Color.WHITE);
        jbtNoColor.addActionListener(new ActionJDColorChooser());
        this.add(jbtNoColor);

        this.setVisible(true);
    }
    private class ActionJDColorChooser implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            componentToChange.setBackground(((JButton)e.getSource()).getBackground());
            if(!e.getActionCommand().equals("noColor")){
                componentToChange.setIcon(null);
            }
            else{
                componentToChange.setIcon(new ImageIcon("./images/pasok.gif"));
            }
                dispose();
        }

    }
}
